package com.qdxht.dispenser.http.model;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;

import com.qdxht.dispenser.R;
import com.qdxht.dispenser.http.exception.TokenException;
import com.qdxht.dispenser.interfaces.IInitData;
import com.qdxht.dispenser.utils.CommonUtil;
import com.qdxht.dispenser.utils.Tools;
import com.qdxht.dispenser.view.DialogSingleUtil;
import com.qdxht.dispenser.view.DialogUtil;
import com.qdxht.dispenser.view.loading.LoadingBar;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;

import okhttp3.Call;

public class HttpListenerImpl<T> implements OnHttpListener<T> {
    private final String TAG = getClass().getSimpleName();

    private Context context;
    private boolean showDialog;
    private boolean isInit;
    private boolean hasMoreInit;
    private IInitData initLayout;

    public HttpListenerImpl(Context context, boolean showDialog) {
        this(context, null, showDialog, false,false);
    }

    public HttpListenerImpl(Context context, boolean showDialog, boolean isInit) {
        this(context, null, showDialog, isInit,false);
    }

    public HttpListenerImpl(Context context, IInitData initLayout, boolean showDialog) {
        this(context, initLayout, showDialog, false,false);
    }

    public HttpListenerImpl(Context context, IInitData initLayout, boolean showDialog, boolean isInit) {
        this(context, initLayout, showDialog, isInit,false);
    }

    /**
     * 参数说明
     *
     * @param context     当请求在Activity中时传入BaseAbstractActivity 对象以便统一实现LOADING及加载重试。
     * @param showDialog  是否显示Dialog，当此参数和isInit同时为true时显示界面内的类似今日头条的Loading，而当showDialog为true而isInit为false时显示弹出框式Loading
     * @param isInit      是否是界面初始化的数据请求，配合showDialog使用，说明参见showDialog参数。
     * @param hasMoreInit 当前界面数据初始化是否还有其他的请求，如果有此参数传true，界面内Loading会一直显示，直到最后一个请求完成。
     */
    public HttpListenerImpl(Context context, IInitData initLayout, boolean showDialog, boolean isInit, boolean hasMoreInit) {
        this.context = context;
        this.initLayout = initLayout;
        this.showDialog = showDialog;
        this.isInit = isInit;
        this.hasMoreInit = hasMoreInit;
    }

    private void checkShowError(Exception response) {
//        if (response == null) {
//            DialogSingleUtil.getInstance().showServerError(context);
//        } else {
//            showErrorDialog(response.getMessage(), context);
//        }
        if (response == null) {
            ToastUtils.show(R.string.the_server_no_respond);
        } else {
            ToastUtils.show(response.getMessage());
        }
    }

    private void showAuthErrorDialog(String msg) {
        DialogUtil.showErrorMessageDialog(context, msg, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                CommonUtil.loginOut();
            }
        });
    }

    private void showErrorDialog(String message, Context context) {
        if (TextUtils.isEmpty(message)) {
            message = context.getResources().getString(R.string.empty_message);
        }
        DialogUtil.showErrorMessageDialog(context, message, context.getResources().getString(R.string.dialog_confirm), null, null, null);
    }

    @Override
    public void onStart(Call call) {
        if(initLayout!=null&&showDialog&&isInit){
            initLayout.showInitLoading();
        }

        if (showDialog && !isInit) {
            if (!LoadingBar.getInstance().isShow()) {
                LoadingBar.getInstance().show(context);
            }
        }

    }

    @Override
    public void onSucceed(T result, boolean cache) {
        onSucceed(result);
    }

    @Override
    public void onSucceed(T result) {
        if(initLayout!=null&&showDialog&&isInit&&!hasMoreInit){
            initLayout.stopInitLoading();
        }
    }

    @Override
    public void onFail(Exception e) {
        //判断是否是请求加载界面的数据,如果是并且请求失败,弹出重试界面点击重试
        hasMoreInit = false;
        if(initLayout!=null&&showDialog&&isInit){
            initLayout.showInitFail();
        }

        if (showDialog && !isInit) {
            LoadingBar.getInstance().cancel();
        }

        if (e == null) {
            return;
        }

        e.printStackTrace();

        if (e instanceof TokenException) {
            showAuthErrorDialog(e.getMessage());
        } else {
            if(!Tools.isNetworkConnected(context)){
                DialogSingleUtil.getInstance().showNoNetWork(context);
            }else{
                checkShowError(e);
            }
        }
    }

    @Override
    public void onEnd(Call call) {
        if (showDialog && !isInit && !hasMoreInit) {
            LoadingBar.getInstance().cancel();
        }
    }

}
